/**
 * @Author 天下无敌
 * @Date 
 * @AIDE AIDE+ 
 */
package com.ecs.core.gsystem.chunk;
import com.artemis.annotations.All;
import com.artemis.BaseEntitySystem;
import com.ecs.core.gsystem.player.PlayerDataSystem;
import com.ecs.core.gsystem.StageSystem;
import com.ecs.core.gsystem.CameraSystem;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
//import static com.ecs.core.GameContext.metersToPixels;
import com.ecs.core.GameContext;
import com.ecs.core.components.map.ChunkCom;
//import com.ecs.core.gcoms.chunk.ChunkPosCom;

@All(ChunkCom.class)
public class ChunkMinMapSystem extends BaseEntitySystem {
    private PlayerDataSystem playerSystem;
    private StageSystem stageSystem;
    private CameraSystem cameraSystem; 

    private final Vector2 currentPos = new Vector2();
    private final Vector2 oldPos = new Vector2(Integer.MIN_VALUE,Integer.MIN_VALUE);
  
    private Image image;
    private boolean isZoomed = false;
    private final Vector2 originalPosition = new Vector2();
    private final Vector2 originalSize = new Vector2();
    private final Vector2 screenCenter = new Vector2();
	
	private final float TEXTURESIZE = GameContext.CHUNK_SIZE * 3;
    private Texture minimapTexture;

    @Override
    protected void initialize() {
        super.initialize();
        image = new Image();
        Stage stage = stageSystem.getStage();
		
        float height = stage.getHeight() * 0.3f;
        float width = height;

        originalSize.set(width, height);;
        originalPosition.set(stage.getWidth() - width - 10, stage.getHeight() - height - 10);

		minimapTexture = new Texture((int)TEXTURESIZE,(int)TEXTURESIZE,Pixmap.Format.RGBA8888);

        image.setSize(originalSize.x, originalSize.y);
        image.setPosition(originalPosition.x, originalPosition.y);

        // 计算屏幕中心
        screenCenter.set(stage.getWidth() / 2f, stage.getHeight() / 2f);
        // 添加点击监听器
        image.addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					toggleZoom();
				}
			});

        stage.addActor(image);
    }

    private void toggleZoom() {
        Stage stage = stageSystem.getStage();
        if (isZoomed) {
            // 返回原始位置和大小
            image.setSize(originalSize.x, originalSize.y);
            image.setPosition(originalPosition.x, originalPosition.y);
        } else {
            // 放大到屏幕中央，保持宽高比
            float maxSize = Math.min(stage.getWidth(), stage.getHeight()) * 0.8f;
            float aspectRatio = originalSize.x / originalSize.y;

            float zoomedWidth, zoomedHeight;
            if (aspectRatio > 1) {
                zoomedWidth = maxSize;
                zoomedHeight = maxSize / aspectRatio;
            } else {
                zoomedHeight = maxSize;
                zoomedWidth = maxSize * aspectRatio;
            }

            image.setSize(zoomedWidth, zoomedHeight);
            image.setPosition(
                screenCenter.x - zoomedWidth/2, 
                screenCenter.y - zoomedHeight/2
            );
        }

        isZoomed = !isZoomed;
        addZoomAnimation();
    }
	private void addZoomAnimation() {
        image.clearActions();
        float duration = 0.15f;

        if (isZoomed) {
            image.addAction(Actions.scaleTo(1.05f, 1.05f, duration/2));
            image.addAction(Actions.sequence(
								Actions.scaleTo(0.95f, 0.95f, duration/2),
								Actions.scaleTo(1f, 1f, duration/4)
							));
        } else {
            image.addAction(Actions.sequence(
								Actions.scaleTo(1.05f, 1.05f, duration/3),
								Actions.parallel(
									Actions.scaleTo(1f, 1f, duration/2),
									Actions.moveTo(originalPosition.x, originalPosition.y, duration/2)
								)
							));
        }
    }

    @Override
    protected void processSystem() {
        updatePlayerPos();

        if(oldPos.epsilonEquals(currentPos, 5f)) {
            return;
        }

        oldPos.set(currentPos);
        updateMinMapTexture();
    }

    private void updatePlayerPos() {
        currentPos.set(playerSystem.getPlayerCenterX(), playerSystem.getPlayerCenterY());
		cameraSystem.updata();
    }

    private void updateMinMapTexture() {
   
		//Pixmap newP = new Pixmap((int)TEXTURESIZE,(int)TEXTURESIZE,Pixmap.Format.RGBA8888);
		
    }

	/*private int isChunkLoaded(int chunkX, int chunkY) {
        for (int i = 0;i < getEntityIds().size();i++) {
			int chunkEntity = getEntityIds().get(i);
            ChunkPosCom pos = world.getEntity(chunkEntity).getComponent(ChunkPosCom.class);
            if (pos.chunkX == chunkX && pos.chunkY == chunkY) {
				return chunkEntity;
            }
        }
		return -1;
    }*/
	
    @Override
    public void dispose() {
        super.dispose();
        if (minimapTexture != null) {
            minimapTexture.dispose();
        }
    }
}

